﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;
using TJADRICD.Model;
using TJADRICD.DLL;
using System.Data.SqlClient;

namespace TJADRICD.CollaborativeDesignPlugin
{

    public class AuthorizeDao
    {
        public void deleteAuthorizeRecord(Authorize auEntity)
        {
            string deleteStr = "delete * from Authorize where MajorID = '" + auEntity.MajorId + "'";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(deleteStr, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);

            }
            

        }
        public void insertAuthorRecord(Authorize authorRecord)
        {
            string insertStr = "insert into Authorize(Role_Major_ID,Auth_User_ID,Start_Date,Finish_Date) Values('" + authorRecord.RoleMajorId +"','" + authorRecord.AuthId +"','" + authorRecord.StartDate.ToString() + "','" + authorRecord.FinishDate.ToString() + "')";
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(insertStr, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
            }
        }
        public List<Authorize> getAuthorListByUser(string userID)
        {
            string selectStr = "select * from Authorize where Role_Major_ID = '" + userID + "'";
            List<Authorize> AuthorList = new List<Authorize>();
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Authorize authorEntity = new Authorize();
                    authorEntity.MajorId = reader[2].ToString();
                    authorEntity.AuthId = reader[1].ToString();
                    authorEntity.RoleMajorId = userID;
                    authorEntity.StartDate = Convert.ToDateTime(reader[3]);
                    authorEntity.FinishDate = Convert.ToDateTime(reader[4]);
                    AuthorList.Add(authorEntity);
                }
                conn.Close();
                reader.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return AuthorList;
            }
            
            return AuthorList;
        }
        
        public List<String> getMajorIDByUser(string userID)
        {
           
            string selectStr = "select * from Authorize where Auth_User_ID =" + userID;
            List<String> mayorIdList = new List<String>();
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    if (isNowInAuthTime(reader[3], reader[4]))
                    {
                        mayorIdList.Add(reader[0].ToString());
                    }
                }
                conn.Close();
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return mayorIdList;
            }
            return mayorIdList;
        }

        //判断是否在授权时间内
        public bool isNowInAuthTime(object start , object end)
        {
            if (DateTime.Compare(Convert.ToDateTime(start), DateTime.Now) <= 0 && DateTime.Compare(Convert.ToDateTime(end), DateTime.Now) >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //autherID授权人ID，userID代签人Id
        public bool isAuthNoCode(string userID , string autherID)
        {
            
            string selectStr = "select * from Authorize where Auth_User_ID = " + userID + " and Role_Mayor_ID = "+ autherID;
            bool isAuth = false;
            try
            {
                SqlConnection conn = Connection.getSqlConnection(CONST.connstr);
                SqlCommand cmd = new SqlCommand(selectStr, conn);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    if (isNowInAuthTime(reader[3], reader[4]))
                    {
                        isAuth = true;
                    }
                }
                conn.Close();
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("获取数据库信息时候发生了错误：" + e.Message);
                return isAuth;
            }
            return isAuth;
        }
    }
}
